package com.android.qjpmc.application_module.home.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.ColorSpace;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.home.fragment.MainPwrDiagramFragment;
import com.android.qjpmc.application_module.home.fragment.ScanStationListFragment;
import com.android.qjpmc.application_module.home.fragment.ViewEventFragment;
import com.android.qjpmc.data_module.ComManager;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.ActivityHomeBinding;
import com.android.qjpmc.manager.FragmentManager;
import com.android.qjpmc.application_module.home.contract.HomeContract;
import com.android.qjpmc.application_module.home.fragment.AlarmRecordslFragment;
import com.android.qjpmc.application_module.home.fragment.HistoryPageFragment;
import com.android.qjpmc.application_module.home.fragment.StationListFragment;
import com.android.qjpmc.application_module.home.fragment.MinePageFragment;
import com.android.qjpmc.application_module.home.viewModel.HomeViewModel;
import com.android.qjpmc.widget.base.BaseFragmentAdapter;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.mvvm.view.BaseMVVMActivity;
import com.android.qjpmc.widget.utils.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.logger.Logger;
import com.zsq.library.BottomMenuItem;
import com.zsq.library.BottomMenuLayout;

import java.util.ArrayList;
import java.util.List;

@CreateViewModel(HomeViewModel.class)
public class HomeActivity extends BaseMVVMActivity<HomeViewModel, ActivityHomeBinding> implements HomeContract.View {


    private ScanStationListFragment scanStationListFragment;

    private class PagerObserver extends DataSetObserver {
        PagerObserver() {
        }

        public void onChanged() {
            //调用Adapter的notifyDataSetChanged()方法时会通知ViewPager类PagerObserver实例的onChanged()方法，然后调用ViewPager.this.dataSetChanged()更新Item
            //ViewPager.this.dataSetChanged();
        }

        public void onInvalidated() {
            //ViewPager.this.dataSetChanged();
        }
    }


    ComManager m_ComManager = new ComManager();
    private List<Fragment> mFragmentList = new ArrayList<>();
    private long exitTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    PagerObserver Observer = new PagerObserver();

    private class ViewPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            //setCurrentItem(position);
            //ToastUtils.showShort("选择第" + Integer.toString(position) + "个页面");
            switch (position) {
                case 0: {
                    //聚焦主接线图页面,采集数据....
                    PwrDataModule.getInstance().m_RtDataEntranceMng.SetCurrentRtDataEntrance(0);
                }
                break;
                default: {
                    PwrDataModule.getInstance().m_RtDataEntranceMng.ClearCurrentRtDataEntrance();
                }
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @Override
    public void initView() {
        super.initView();

        //加载站列表参数
        m_ComManager.StartComThread();


        ImmersionBar.with(HomeActivity.this).statusBarColor(R.color.color_244395).navigationBarColor(R.color.white).fitsSystemWindows(true).statusBarDarkFont(false, 0.2f).init();

        //MainPwrDiagramFragment main_pwr_fragment = (MainPwrDiagramFragment) FragmentManager.GetMainPwrDiagramFragment();
        // StationListFragment AllStationFragment = FragmentManager.getStationListFragment();
        scanStationListFragment = FragmentManager.getScanStationListFragment();
        // AlarmRecordslFragment alarmRecordslFragment = FragmentManager.getAlarmRecordFragment();
        // HistoryPageFragment historyDataFragment = FragmentManager.getHistoryPageFragment();
        // ViewEventFragment EventFragment = FragmentManager.getViewEventFragment();

        //MinePageFragment minePageFragment = FragmentManager.getMinePageFragment();


        // mFragmentList.add(main_pwr_fragment);
        mFragmentList.add(scanStationListFragment);
        //  mFragmentList.add(alarmRecordslFragment);
        // mFragmentList.add(EventFragment);

        // mFragmentList.add(historyDataFragment);
        //mFragmentList.add(minePageFragment);

        BaseFragmentAdapter adapter = new BaseFragmentAdapter(getSupportFragmentManager(), mFragmentList, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setOffscreenPageLimit(mFragmentList.size());
        mBinding.bbl.setViewPager(mBinding.viewPager);

        mBinding.viewPager.setOnPageChangeListener(new ViewPageChangeListener());
        //mBinding.viewPager.s
        //adapter.setViewPagerObserver(Observer);


    }

    @Override
    public void onBackPressed() {
        if (mBinding != null && mBinding.bbl.getCurrentItem() == 0) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                ToastUtils.showShort(R.string.Click_again_to_return_desktop);
                exitTime = System.currentTimeMillis();
            } else {
                // System.exit(0)
                moveTaskToBack(true);
                Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
                launcherIntent.addCategory(Intent.CATEGORY_HOME);
                startActivity(launcherIntent);

            }
        } else if (mBinding != null && mBinding.bbl.getCurrentItem() != 0) {
            mBinding.bbl.setCurrentItem(0);
        } else {
            moveTaskToBack(true);
            super.onBackPressed();
        }
    }

    @Override
    public void initEvent() {
        super.initEvent();
        mBinding.bbl.setBottomMenuOnPageChangeListener(new BottomMenuLayout.BottomMenuOnPageChangeListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onPageSelected(BottomMenuItem bottomMenuItem, int previousPosition, int currentPosition) {
                // mBinding.bbl.
                //mBinding.systemGraphItem.setBackgroundColor(android.R.color.holo_blue_light);
                //mBinding.stationItem.setBackgroundColor(android.R.color.holo_blue_light);
                //mBinding.alarmItem.setBackgroundColor(android.R.color.holo_blue_light);
                //mBinding.historyItem.setBackgroundColor(android.R.color.holo_blue_light);
/*
                switch (currentPosition) {
                    case 0:
                    case 2:

                    case 1:
                    case 3:
                       // mBinding.bbl.setT
                        ImmersionBar.with(HomeActivity.this).statusBarColor(R.color.color_244395).navigationBarColor(R.color.black_04).fitsSystemWindows(true).statusBarDarkFont(false, 0.2f).init();

                        break;

                }
                switch (currentPosition)
                {

                    case 0:
                    {
                        //mBinding.systemGraphItem.setTabSelect(true); // setBackgroundColor(android.R.color.holo_orange_light);.
                        mBinding.systemGraphItem.getTextView().setTextColor(android.R.color.holo_orange_light);//(android.R.color.background_dark);
                        mBinding.systemGraphItem.getTextView().setText("系统图");
                    }
                    break;
                    case 2:
                    {
                        mBinding.stationItem.setTabSelect(true);//setBackgroundColor(android.R.color.holo_orange_light);
                    }
                    break;
                    case 1:
                    {
                        mBinding.alarmItem.setTabSelect(true);//setBackgroundColor(android.R.color.holo_orange_light);
                    }
                    break;
                    case 3:
                    {
                        mBinding.historyItem.setTabSelect(true);//setBackgroundColor(android.R.color.holo_orange_light);
                    }
                    break;

                }*/
            }
        });
    }

    @Override
    protected boolean isNeedLoadStatusBar() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        scanStationListFragment.onActivityResult(requestCode, resultCode, data);
    }
}
